package com.example.mylistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
    HorizontalListView hListView;
    HorizontalListViewAdapter hListViewAdapter;
    ImageView previewImg;
    View olderSelectView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void initUI() {
        hListView = (HorizontalListView) findViewById(R.id.horizon_listview);
        previewImg = (ImageView) findViewById(R.id.image_preview);
        String[] titles = {"��ʦ", "�ϻ�誾�У", "�չ�", "�ϻ��", "�Ϲ�ׯ����", "��ʦ����"
                , "��ʦ", "�ϻ�誾�У", "�չ�", "�ϻ��", "�Ϲ�ׯ����", "��ʦ����", "��ʦ", "�ϻ�誾�У", "�չ�", "�ϻ��", "�Ϲ�ׯ����", "��ʦ����"
        };
        final int[] ids = {R.drawable.nanhuaijin_miss, R.drawable.nanhuaijin_school,
                R.drawable.nanhuaijin_biguan, R.drawable.nanhuaijin,
                R.drawable.nanhuaijin_zhuangyan, R.drawable.nanhuaijin_faxiang,R.drawable.nanhuaijin_miss, R.drawable.nanhuaijin_school,
                R.drawable.nanhuaijin_biguan, R.drawable.nanhuaijin,
                R.drawable.nanhuaijin_zhuangyan, R.drawable.nanhuaijin_faxiang,R.drawable.nanhuaijin_miss, R.drawable.nanhuaijin_school,
                R.drawable.nanhuaijin_biguan, R.drawable.nanhuaijin,
                R.drawable.nanhuaijin_zhuangyan, R.drawable.nanhuaijin_faxiang};
        hListViewAdapter = new HorizontalListViewAdapter(getApplicationContext(), titles, ids);
        hListView.setAdapter(hListViewAdapter);
        hListView.setFocusableInTouchMode(true);
        //		hListView.setOnItemSelectedListener(new OnItemSelectedListener() {
        //
        //			@Override
        //			public void onItemSelected(AdapterView<?> parent, View view,
        //					int position, long id) {
        //				// TODO Auto-generated method stub
        //				if(olderSelected != null){
        //					olderSelected.setSelected(false); //��һ��ѡ�е�View�ָ�ԭ����
        //				}
        //				olderSelected = view;
        //				view.setSelected(true);
        //			}
        //
        //			@Override
        //			public void onNothingSelected(AdapterView<?> parent) {
        //				// TODO Auto-generated method stub
        //
        //			}
        //		});
        hListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
//				if(olderSelectView == null){
//					olderSelectView = view;
//				}else{
//					olderSelectView.setSelected(false);
//					olderSelectView = null;
//				}
//				olderSelectView = view;
//				view.setSelected(true);
                previewImg.setImageResource(ids[position]);
                hListViewAdapter.setSelectIndex(position);
                hListViewAdapter.notifyDataSetChanged();

            }
        });

    }

}
